$(function(){	
	init();
	bindFun();
	cancer();
});
//全局变量 保单号
var policyRef;
var contractId;
function init(){
	policyRef = sessionStorage.policyRef;
	$("#policyRef").html(policyRef);
	contractId = sessionStorage.contractId;
	initPic();
	//长按图片删除事件
	doubleDelete();
}


function initPic(){
	var typeList=[];
	typeList.push("1160");
	typeList.push("1163");
	typeList.push("1167");
	typeList.push("1169");
	typeList.push("1175");
	padMarketing.ajaxNormal(
			"/pad_marketing/qualityCheck/getPic.action",
			{	'typeList' : typeList,
				'policyRef':policyRef,
				'contractId':contractId},
			function(data){
						if(data.M&&data.M.length!=0){
							var ML = data.M;
							for (var j in ML)
							{
								var flag1=getNum(ML[j],"M")
								addPic("M",flag1);
								$("#"+("imgM")+flag1).attr("src","/pad_marketing/qualityCheck/showPic.action?type="+("M_")+flag1+"&policyRef="+policyRef+"&contractId="+contractId);
							}
						}else{
							addPic("M");
						}
						if(data.O&&data.O.length!=0){
							var OL = data.O;
							for (var k in OL )
							{
								var flag2= getNum(OL[k],"O");
								addPic("O",flag2);
								$("#"+("imgO")+flag2).attr("src","/pad_marketing/qualityCheck/showPic.action?type="+("O_")+flag2+"&policyRef="+policyRef+"&contractId="+contractId);
							}
						}else{
							addPic("O");
						}
						if(data.I&&data.I.length!=0){
							var IL = data.I;
							for (var k in IL )
							{
								var flag2= getNum(IL[k],"I");
								addPic("I",flag2);
								$("#"+("imgI")+flag2).attr("src","/pad_marketing/qualityCheck/showPic.action?type="+("I_")+flag2+"&policyRef="+policyRef+"&contractId="+contractId);
							}
						}else{
							addPic("I");
						}
						if(data.N&&data.N.length!=0){
							var NL = data.N;
							for (var k in NL )
							{
								var flag2= getNum(NL[k],"N");
								addPic("N",flag2);
								$("#"+("imgN")+flag2).attr("src","/pad_marketing/qualityCheck/showPic.action?type="+("N_")+flag2+"&policyRef="+policyRef+"&contractId="+contractId);
							}
						}else{
							addPic("N");
						}
						if(data.X&&data.X.length!=0){
							var XL = data.X;
							for (var k in XL )
							{
								var flag2= getNum(XL[k],"X");
								addPic("X",flag2);
								$("#"+("imgX")+flag2).attr("src","/pad_marketing/qualityCheck/showPic.action?type="+("X_")+flag2+"&policyRef="+policyRef+"&contractId="+contractId);
							}
						}else{
							addPic("X");
						}
						checkHtmlNull();
					}
	);
}



function bindFun(){
	$("#next").click(function(){
		var typeList=[];
		typeList.push("1160");
		typeList.push("1163");
		typeList.push("1167");
		typeList.push("1169");
		typeList.push("1175");
		padMarketing.ajaxNormal(
				"/pad_marketing/qualityCheck/getPic.action",
				{	'typeList' : typeList,
					'policyRef':policyRef,
					'contractId':contractId},
				function(data){
						if(data.I){
							location.href="affirmNoteReply.action?I="+data.I.length+"&N="+data.N.length+"&X="+data.X.length+"&M="+data.M.length+"&O="+data.O.length+"&contractId="+contractId+"&policyRef="+policyRef+"&url=furtherInformation.action";
							return;
						}else{
							alertMsg("请您补充资料");
							return;
						}
						
					}
		);
	});
}
var photoNo=null;
var type=null;
//图片点击,调用壳的方法
function takePic(tpe,no){
	photoNo=no;
	type=tpe;
	closeDiv('open0');
	$("#MyPhoto").attr("onclick","goTakePhoto('"+type+"')");
	$("#MyAlbum").attr("onclick","openLibrary('"+type+"')");
	showDiv('open0');
}
//壳返回调用的方法 财务类
function M(i) {
	var base64 = 'data:image/png;base64,' + i;
	var idType="M_"+photoNo;
	closeDiv('open0');
	uploadOne(base64, idType,policyRef);
}
//壳返回调用的方法 其他类
function O(i) {
	var base64 = 'data:image/png;base64,' + i;
	var idType="O_"+photoNo;
	closeDiv('open0');
	uploadOne(base64, idType,policyRef);
}
//壳返回调用的方法 其他类
function I(i) {
	var base64 = 'data:image/png;base64,' + i;
	var idType="I_"+photoNo;
	closeDiv('open0');
	uploadOne(base64, idType,policyRef);
}
//壳返回调用的方法 其他类
function N(i) {
	var base64 = 'data:image/png;base64,' + i;
	var idType="N_"+photoNo;
	closeDiv('open0');
	uploadOne(base64, idType,policyRef);
}
//壳返回调用的方法 其他类
function X(i) {
	var base64 = 'data:image/png;base64,' + i;
	var idType="X_"+photoNo;
	closeDiv('open0');
	uploadOne(base64, idType,policyRef);
}
//图片单独上传
function uploadOne(imageData, idType,policyRef) {
	padMarketing.ajaxNormal(
			"/pad_marketing/qualityCheck/uploadOne.action",
			{	'image' : imageData,
				'idType' : idType,
				'policyRef':policyRef,
				'contractId':contractId},
			function(data){
					$("#img"+type+photoNo).attr('src',imageData);
				}
	);
}
//长按图片删除事件
function doubleDelete(){
	var t = null;
	$("input[class='input']").live('touchstart',function(target){
		t = setTimeout(function(){
			alertMsg("确定删除该照片？",1,function(){
				var src = $(target.target).next().attr("src");
				if(src==""){
					$(target.target).parent().remove();
					checkHtmlNull();
				}else{
					var id = $(target.target).attr("id");
					id=id.substring(0,1)+'_'+id.substring(1,id.length);
					padMarketing.ajaxNormal(
							"/pad_marketing/qualityCheck/deletePic.action",
							{	'id' : id,
								'policyRef':policyRef,
								'contractId':contractId},
							function(data){
									$(target.target).parent().remove();
									checkHtmlNull();
								}
					);
				}
				

				
			});
		},1000);
	});
	$("input[class='input']").live('touchend',function(){
		clearTimeout(t);
	});
}

//点击增加上传框
var o      =1;
var m      =1;
var i      =1;
var n      =1;
var x      =1;
function addPic(type,num){
	var html="";
	m = $("input[name='M']").length;
	o = $("input[name='O']").length;
	i = $("input[name='i']").length;
	n = $("input[name='N']").length;
	x = $("input[name='X']").length;
	 if(type=="M"){
		if(num){
			m=num;
		}else{
			var max = getMaxNum(type)
			m++;
			if(m==max){
				m++;
			}
		}
		html+='<div class="upload-img pull-left">';
		html+='<input type="file" id='+("M"+m)+' name="M" class="input" onclick="takePic('+"'M'"+','+m+')">';
		html+='<img src="" id='+("imgM"+m)+'>';
		html+='<h5>';
		html+='<p class="gray font-16 lh-30">请原件拍照或上传</p>';
		html+=' </h5>';
		html+='</div>';
		$("#MA").before(html);
	}else if(type=="O"){
		if(num){
			o=num;
		}else{
			var max = getMaxNum(type)
			o++;
			if(o==max){
				o++;
			}
		}
		html+='<div class="upload-img pull-left">';
		html+='<input type="file" id='+("O"+o)+' name="O" class="input" onclick="takePic('+"'O'"+','+o+')">';
		html+='<img src="" id='+("imgO"+o)+'>';
		html+='<h5>';
		html+='<p class="gray font-16 lh-30">请原件拍照或上传</p>';
		html+=' </h5>';
		html+='</div>';
		$("#OA").before(html);
	}else if(type=="I"){
		if(num){
			i=num;
		}else{
			var max = getMaxNum(type)
			i++;
			if(i==max){
				i++;
			}
		}
		html+='<div class="upload-img pull-left">';
		html+='<input type="file" id='+("I"+i)+' name="I" class="input" onclick="takePic('+"'I'"+','+i+')">';
		html+='<img src="" id='+("imgI"+i)+'>';
		html+='<h5>';
		html+='<p class="gray font-16 lh-30">特申:请原件拍照或上传</p>';
		html+=' </h5>';
		html+='</div>';
		$("#IA").before(html);
	}else if(type=="N"){
		if(num){
			n=num;
		}else{
			var max = getMaxNum(type)
			n++;
			if(n==max){
				n++;
			}
		}
		html+='<div class="upload-img pull-left">';
		html+='<input type="file" id='+("N"+n)+' name="N" class="input" onclick="takePic('+"'N'"+','+n+')">';
		html+='<img src="" id='+("imgN"+n)+'>';
		html+='<h5>';
		html+='<p class="gray font-16 lh-30">请原件拍照或上传</p>';
		html+=' </h5>';
		html+='</div>';
		$("#NA").before(html);
	}else if(type=="X"){
		if(num){
			x=num;
		}else{
			var max = getMaxNum(type)
			x++;
			if(x==max){
				x++;
			}
		}
		html+='<div class="upload-img pull-left">';
		html+='<input type="file" id='+("X"+x)+' name="X" class="input" onclick="takePic('+"'X'"+','+x+')">';
		html+='<img src="" id='+("imgX"+x)+'>';
		html+='<h5>';
		html+='<p class="gray font-16 lh-30">请原件拍照或上传</p>';
		html+=' </h5>';
		html+='</div>';
		$("#XA").before(html);
	}
	cancer();
	if(myScroll)
		myScroll.refresh(); 
}
//获得界面上 同一类型最大的图片
function getMaxNum(type){
	var max=0;
	$("input[name=\'"+type+"\']").each(function(index,me){
		 var id = parseInt(($(me).attr("id")).replace(type,""));
		 if(max<id){
			 max=id;
		 }
	});
	return max;
} 
//获得地址中的参数
function getNum(name,type){
	var arr = name.split("_")[2];
	return parseInt(arr);
}
// 刷新界面防止界面出现没有拍照框的情况
function checkHtmlNull(){
	var html ="";
	html=$("#MAll").html();
	if("-1"==html.indexOf("class")){
		addPic("M");
	}
	
	html=$("#OAll").html();
	if("-1"==html.indexOf("class")){
		addPic("O");
	}
	
	html=$("#IAll").html();
	if("-1"==html.indexOf("class")){
		addPic("I");
	}
	
	html=$("#NAll").html();
	if("-1"==html.indexOf("class")){
		addPic("N");
	}
	
	html=$("#XAll").html();
	if("-1"==html.indexOf("class")){
		addPic("X");
	}
}
//返回
function returnUrl(){
	var da={};
	da.contractId=contractId;
		padMarketing.ajaxNormal(
				"/pad_marketing/qualityCheck/initMyContent.action",
				da,
				function(data){
					if(data.flag=="01"){
						location.href="/pad_marketing/index/index.action";
					}else{
						result = data.result;
							if(result.tb=="Y" || result.other=="Y" ){
								location.href="bodySign.action?contractId="+contractId+"&policyRef="+policyRef;
							}else{
								location.href="noteDetailQC.action?contractId="+contractId+"&policyRef="+policyRef;
							}
					}
				}
		);
}
function cancer(){
	$("input[type=file]").click(function(event) {
        event.preventDefault();
      });
}